package org.design.utils;

import lombok.Data;

/**
 * R
 *
 * @author 樊温军
 * @date 2025/6/23 下午2:21
 */
@Data
public class R<T> {

    private static final String DEFAULT_SUCCESS_MESSAGE = "SUCCESS";
    private static final String DEFAULT_FAIL_MESSAGE = "FAIL";
    private static final int RESULT_CODE_SUCCESS = 0;
    private static final int RESULT_CODE_FAIL = 1;
    
    private String msg;
    private int code;
    private T data;

    public R() {
    }

    public static <T> R<T> ok() {
        R<T> r = new R<>();
        r.setCode(RESULT_CODE_SUCCESS);
        r.setMsg(DEFAULT_SUCCESS_MESSAGE);
        return r;
    }

    public static <T> R<T> ok(T data) {
        R<T> r = new R<>();
        r.setCode(RESULT_CODE_SUCCESS);
        r.setMsg(DEFAULT_SUCCESS_MESSAGE);
        r.setData(data);
        return r;
    }

    public static <T> R<T> ok(T data, String msg) {
        R<T> r = new R<>();
        r.setCode(RESULT_CODE_SUCCESS);
        r.setMsg(msg);
        r.setData(data);
        return r;
    }

    public static <T> R<T> fail() {
        return fail(DEFAULT_FAIL_MESSAGE);
    }

    public static <T> R<T> fail(String msg) {
        R<T> r = new R<>();
        r.setCode(RESULT_CODE_FAIL);
        r.setMsg(msg);
        return r;
    }

    public static <T> R<T> fail(T data, String msg) {
        R<T> r = new R<>();
        r.setCode(RESULT_CODE_FAIL);
        r.setMsg(msg);
        r.setData(data);
        return r;
    }
    
}
